<template>
  <div>
    <div class="header">

      <van-nav-bar
        title="宣教视频"
        style="background: #fff;"
        left-arrow
        @click-left="onClickLeft"
      />

    </div>
    <div>
      <video-player  class="video-player vjs-custom-skin"
                     ref="videoPlayer" :playsinline="false"
                     :options="playerOptions"
                     @play="onPlayerPlay($event)"
                     @pause="onPlayerPause($event)"
                     @ended="onPlayerEnded($event)"
                     @loadeddata="onPlayerLoadeddata($event)"
                     @waiting="onPlayerWaiting($event)"
                     @playing="onPlayerPlaying($event)"
                     @timeupdate="onPlayerTimeupdate($event)"
                     @canplay="onPlayerCanplay($event)"
                     @canplaythrough="onPlayerCanplaythrough($event)"

                     @statechanged="playerStateChanged($event)"
                      >

      </video-player>
    </div>
  </div>

</template>


<script>
    import { videoPlayer } from 'vue-video-player';
    import 'video.js/dist/video-js.css';
    export default {
        data(){
            return{
                time:0,
                video_url:'',
                edu_pic_url:'',
                userId:'',
                integral:'',
                educationId:'',

                  playerOptions : {
                      playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
                      autoplay: true, //如果true,浏览器准备好时开始回放。
                      muted: false, // 默认情况下将会消除任何音频。
                      loop: false, // 导致视频一结束就重新开始。
                      preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
                      language: 'zh-CN',
                      aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
                      fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
                      sources: [{
                        src: '',  // 路径
                        type: 'video/mp4'  // 类型
                      }],
                      poster: "", //你的封面地址
                      // width: document.documentElement.clientWidth,
                      notSupportedMessage: '此视频暂无法播放，请稍后再试', //允许覆盖Video.js无法播放媒体源时显示的默认信息。
                      controls:true,
                      controlBar: {
                        timeDivider: true,
                        durationDisplay: true,
                        remainingTimeDisplay: false,//剩余时间
                        fullscreenToggle: true  //全屏按钮
                      }
                  }

            }
        },
        mounted(){
            //http://qiniu.yaruijie.com/85872ed2b7dbb63a302aa4b251fa0084.mp4
            //https://img.yzcdn.cn/vant/apple-1.jpg

          const that = this;
          that.video_url = that.$route.query.video_url;
          that.edu_pic_url = that.$route.query.edu_pic_url;
          that.userId = that.$route.query.userId;
          that.integral = that.$route.query.integral;
          that.educationId = that.$route.query.educationId;

          that.playerOptions.sources[0].src = that.video_url;
          that.playerOptions.poster = that.edu_pic_url;

        },
        methods: {
            onClickLeft() {
                this.$router.back(-1);
            },
            // listen event
            onPlayerPlay(player) {
                //console.log('player play!', player)
            },
            onPlayerPause(player) {
                //console.log('player pause!', player)
            },
            onPlayerEnded(player) {
                 console.log('player ended!', player);
                 const that = this;
                 let param ="userId="+that.userId+"&integral="+that.integral+"&educationId="+that.educationId;
                that.$sendRequest('/education/save_EducationIntegral',param).then(function(res){
                    console.log(res.data);
                    if(res.data.code ==0){

                    }
                })


            },
            onPlayerLoadeddata(player) {
                 //console.log('player Loadeddata!', player)
            },
            onPlayerWaiting(player) {
                 //console.log('player Waiting!', player)
            },
            onPlayerPlaying(player) {
                 //console.log('player Playing!', player)
            },
            onPlayerTimeupdate(player) {
                 console.log('player Timeupdate!', player.currentTime())
                if(player.currentTime() - this.time >1){
                    player.currentTime(this.time);
                }else{
                    this.time = player.currentTime();
                }

            },
            onPlayerCanplay(player) {
                 //console.log('player Canplay!', player)
            },
            onPlayerCanplaythrough(player) {
                 //console.log('player Canplaythrough!', player)
            },
            // or listen state event
            playerStateChanged(playerCurrentState) {
                 //console.log('player current update state', playerCurrentState)
            },
            /*// player is ready
            playerReadied(player) {
                // seek to 10s
                console.log('example player 1 readied', player)
                player.currentTime(10)
                 console.log('example 01: the player is readied', player)
            }*/
        },
        components: {
            videoPlayer
        }
    }

</script>

<style>
.van-nav-bar .van-icon {
    color: #2c3e50;
}
</style>
